package com.flower8.controller;

import com.flower8.dto.CreditLinkDto;
import com.flower8.model.CreditLink;
import com.flower8.service.CreditLinkService;
import com.flower8.sysconfig.constants.MsgConstants;
import com.flower8.sysconfig.constants.SysConstants;
import com.flower8.utils.MD5;
import com.flower8.utils.PageData;
import com.flower8.utils.ftpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Description: 银行链接控制层
 * @Author: tanglin
 * @Date: 2019/09/25 16:09
 * @Version: 1.0
 */
@RestController
@RequestMapping("/creditLink")
public class CreditLinkController extends BaseController{
    private static final Logger log = LoggerFactory.getLogger(CreditLinkController.class);

    @Autowired
    private CreditLinkService creditLinkService;

    /**
     * 查询所有银行连接列表
     * @param request
     * @return
     */
    @RequestMapping("/getCreditLinkList")
    public Object getCreditLinkList(HttpServletRequest request){
        logBefore(log, "++++++++++ 查询所有银行连接列表 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<CreditLinkDto> data =  creditLinkService.getCreditLinkList(pd);
            int total = creditLinkService.getCreditLinkListCount(pd);
            res.put("data", data);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 根据id查询银行连接
     * @param id
     * @return
     */
    @RequestMapping("/getCreditLinkById")
    public Object getCreditLinkById(@RequestParam long id){
        logBefore(log, "++++++++++ 根据id："+id+"查询银行连接 ++++++++++");

        Map res = new HashMap();
        try {
            CreditLinkDto creditLink = creditLinkService.getCreditLinkById(id);
            res.put("creditLink", creditLink);
            logAfter(log);
            return responseResultForSuccess(res);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 删除银行连接
     * @param id
     * @return
     */
    @RequestMapping("/deleteCreditLink")
    public Object deleteCreditLink(@RequestParam long id){
        logBefore(log, "++++++++++ 删除银行连接 ++++++++++");
        try {
            creditLinkService.deleteCreditLink(id);
            logAfter(log);
            return responseResultForSuccess(null, "删除成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("删除失败");
        }
    }

    /**
     * 更新/新增银行连接
     * @param creditLink
     * @return
     */
    @RequestMapping("/updateCreditLink")
    public Object updateCreditLink(@RequestBody CreditLink creditLink){
        try {
            if(creditLink.getId() != null){
                logBefore(log, "++++++++++ 更新银行连接 ++++++++++");
                creditLinkService.updateCreditLink(creditLink);
            }else{
                logBefore(log, "++++++++++ 新增银行连接 ++++++++++");
                creditLinkService.addCreditLink(creditLink);
            }

            logAfter(log);
            return responseResultForSuccess(null, "操作成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError("操作失败");
        }
    }

}
